#!/usr/bin/env python
# encoding: utf-8
'''
@author: liuchong
@contact: lucas@007vin.com
@software: garner
@file: banzhou.py
@time: 2020/8/24 2:44 下午
@desc:
'''

import sys
sys.path.append('../../')
import pandas as pd
import re
from pymongo import MongoClient
import ujson
from models.skoda.skoda import CarsPartNumNew, CarsSaDesc, CarsStructureCollectionNew
from models.skoda.skoda_vb import Vb
from scripts.fenci import remove_tt
from cars_scripts.service.AudiService import cleanTransmission, cleanDrive, cleanCylinder, cleanPower
from cars_scripts.service.add_date import audi_do_filter_extra_info, mergeDate

s = ['3缸', '三缸', '4缸', '四缸', '5缸', '五缸', '6缸', '六缸', '8缸', '八缸', '10缸', '十缸', '12缸', '十二缸']
qd = ['全轮驱动', '前驱动', '前部驱动', '四轮驱动']
fuel = ['汽油', '柴油']
bsx = ['7档', '7档自动', '7档手动', '7档双离合', '七档', '七档自动', '七档手动', '七档双离合', '6档', '6档自动', '6档手动', '6挡手自一体',
       '六档', '六档自动', '六档手动', '5档', '5档自动', '5档手动', '五档', '五档自动', '五档手动', '5挡手自一体',
       '4档', '4档自动', '4档手动', '四档', '四档自动', '四档手动', '8档', '8档自动', '8档手动', '8挡手自一体',
       '八档', '八档自动', '八档手动', '3档', '3档自动', '3档手动', '三档', '三档自动', '三档手动', '无级自动']
pl = ['1.6', '1.9', '1.3', '1.8', '1.4', '2.0', '1.0', '1.2', '2.8', '2.5', '1.5', '3.6']
fdjdm = ['AEE', 'AEF', 'AMG', 'AMH', 'AMJ', 'AEH', 'AGN', 'AGP', 'AGR', 'AGU', 'AHF', 'AKL', 'AMD', 'APK', 'AQY', 'ARZ', 'ASV', 'AUM', 'AZH', 'AME', 'AMF', 'AQV', 'AQW', 'ARV', 'ASY', 'ASZ', 'ATD', 'ATZ', 'AUA', 'AUB', 'AWY', 'AZE', 'AZF', 'AZL', 'AZQ', 'BBY', 'BBZ', 'BKY', 'BMD', 'AEG', 'ALH', 'AQM', 'ARX', 'AUQ', 'AVU', 'AXP', 'AXR', 'AZJ', 'BCA', 'BFQ', 'AMX', 'AVB', 'AVF', 'AWT', 'AWX', 'AYM', 'AZM', 'BBG', 'BDG', 'BPZ', 'BSS', 'BSV', 'BWW', 'AZV', 'BGU', 'BJB', 'BKC', 'BKD', 'BLF', 'BLR', 'BLS', 'BLX', 'BLY', 'BMM', 'BMN', 'BSE', 'BSF', 'BUD', 'BVX', 'BVY', 'BVZ', 'BWA', 'BXE', 'BZB', 'CCSA', 'CEGA', 'CSSA', 'DJNB', 'DJSA', 'DLSA', 'CZCA', 'CZDA', 'CZPA', 'DBGC', 'BLT', 'BME', 'BNM', 'BNV', 'BMS', 'BSW', 'BTS', 'BXW', 'BZG', 'CGPA', 'EBMA', 'CWVA', 'DADA', 'DBYA', 'DGTA', 'DGTD', 'DKLA', 'DKRF', 'DLAA', 'DLAC', 'DPCA', 'CRVC', 'CZPB', 'DGEA', 'DHFA', 'DJKA', 'DLBA', 'DNNA', 'DNPA', 'DSTA', 'DSTB', 'DSUD', 'DTRD', 'DTTA', 'DTTC', 'DTUA', 'DLFA', 'DLXA', 'BBM', 'CEVA', 'CHFA', 'CHTA', 'BXX', 'BYJ', 'CDFA', 'CEAA', 'CFBA', 'CGMA', 'CGZA', 'CLRA', 'CPJA', 'BMP', 'CAXC', 'CAYC', 'CBBB', 'CCZA', 'CDAA', 'CDAB', 'CDVA', 'CFFB', 'CFGB', 'CLJA', 'CUFA', 'CUGA', 'DBFC', 'DKVB', 'DKXA', 'DPLA', 'CDDA', 'CDEA', 'CLPA', 'CLSA', 'CAXA', 'CBZB', 'CFHC', 'CFHF', 'CGGA', 'CHGA', 'CLCA', 'CLCB', 'CMXA', 'CBDB', 'CFHA', 'CFJA', 'CAVE', 'CAYA', 'CAYB', 'CBZA', 'CFNA', 'CFWA', 'CGGB', 'CGPB', 'CJLB', 'CTHE', 'CJLA', 'CLNA', 'CWXB', 'CWXC', 'DSHA', 'CHYA', 'CHYB', 'CPGA', 'CGPC', 'CHZB', 'CHZC', 'CJZC', 'CJZD', 'CUSB', 'CWVB', 'CXMA', 'DKLD', 'DKRC', 'CHHA', 'CHHB', 'CHPA', 'CHPB', 'CHZD', 'CJSA', 'CJSB', 'CJZA', 'CJZB', 'CKFB', 'CKFC', 'CLHA', 'CLHB', 'CPWA', 'CRKA', 'CRKB', 'CRMB', 'CUNA', 'CUPA', 'CXXA', 'CXXB', 'CYKA', 'CYVA', 'CYVB', 'DBKA', 'CKAA', 'CPDA', 'CSRA', 'CUUA', 'CUUB', 'DFSA', 'DFSB', 'CSTA', 'DLWA', 'DMBA', 'CHYE', 'CUSA', 'CUTA', 'CRGB', 'CUAA', 'CZEA', 'DFGA', 'DFGC', 'DFHA', 'DKZA', 'DTSB', 'DCYA', 'DDYA', 'DDYB', 'DFFA', 'DGTB', 'DGTE', 'DHGA', 'DJGA', 'DKRA', 'DKTB', 'CHZJ', 'CJSC', 'CJXA', 'CRGA', 'CRLB', 'CVRA', 'DCXA', 'DCZA', 'DDAA', 'DFCA', 'DFEA', 'DGEB', 'DLKB', 'DNFE', 'DNUA', 'DSRA', 'DSRB', 'DSUB', 'DTRA', 'DTSA', 'DAHA']
glv = ['55KW', '47KW', '40KW', '50KW', '43KW', '74KW', '92KW', '66KW', '110KW', '81KW', '44KW', '85KW', '37KW', '96KW', '132KW', '75KW', '142KW', '114KW', '140KW', '120KW', '103KW', '77KW', '91KW', '100KW', '125KW', '59KW', '147KW', '118KW', '51KW', '63KW', '61KW', '70KW', '105KW', '180KW', '79KW', '88KW', '112KW', '191KW', '162KW', '137KW', '90KW', '169KW', '135KW', '83KW', '130KW', '176KW', '206KW', '115KW', '200KW']
items = []
client = MongoClient('192.168.10.170', 27017)
db = client.standard_label
collection = db.standard_label_unique
audiParts = CarsPartNumNew.select()
for audiPart in audiParts:
    if audiPart.pid != '' and ('dropped' not in audiPart.extra_info):
        pid = audiPart.pid
        cid = audiPart.cid
        mid = audiPart.mid
        stand_info = collection.find_one({'pid': pid, 'brandCode': audiPart.brand_code})
        stand_label = stand_info['standard_label'] if stand_info else ''
        if stand_label:
            # 配置代码
            sa_codes = []
            # 发动机排量
            cylinder = []
            # 发动机代码
            vb = []
            # 发动机排量信息
            vb_desc = []
            transmission = []
            drive = []
            diff_mech = []
            energy_type = []
            power = []
            # pSacodes = getSacode(audiPart.sa_code)
            part_sa_codes = audiPart.sa_code or '{}'
            part_sa_codes = ujson.loads(part_sa_codes)
            for key, value in part_sa_codes.items():
                if value:
                    sa_codes = sa_codes + value
            pModel = audiPart.model.replace('<br/>', '')
            pRemark = audiPart.remark

            pModelout = remove_tt(pModel)
            pRemarkout = remove_tt(pRemark)

            if pRemark:
                for key in s:
                    if key in pRemarkout:
                        cy = cleanCylinder(key)
                        cylinder.append(cy)
                for key in pl:
                    if key in pRemark:
                        vb_desc.append(key)

            if pModel:
                for key in bsx:
                    if key in pModelout:
                        tran = cleanTransmission(key)
                        transmission.append(tran)
                for key in qd:
                    if key in pModelout:
                        dr = cleanDrive(key)
                        drive.append(dr)

                for key in s:
                    if key in pModelout:
                        mcy = cleanCylinder(key)
                        cylinder.append(mcy)
                for key in pl:
                    if key in pModel:
                        vb_desc.append(key)
                for key in fdjdm:
                    if key in pModel:
                        vb.append(key)
                if 'PR:' in pModel or 'PR-' in pModel or 'pr:' in pModel or 'pr-' in pModel:
                    # 处理part_model中的PR
                    part_pr = re.sub('[\u4e00-\u9fa5]', '', pModel)
                    part_pr = part_pr.replace('PR:', '')
                    part_pr = part_pr.replace('PR-', '')
                    part_pr = part_pr.replace('+', ',')
                    part_pr = part_pr.replace('/', ',')
                    part_pr = part_pr.replace(':', ',')
                    part_pr = part_pr.replace('_', ',')
                    part_pr = part_pr.split(',')
                    for j in part_pr:
                        sa_codes.append(j)

            sa_codes = set(sa_codes)
            sa_codes = list(sa_codes)

            vb = set(vb)
            vb = list(vb)

            for i in sa_codes:
                if cid.startswith('U'):
                    contrast = CarsSaDesc.get_or_none(CarsSaDesc.cid == cid[1:], CarsSaDesc.sa == i,
                                                      CarsSaDesc.lang == 'zh', CarsSaDesc.brand_code == audiPart.brand_code)
                else:
                    contrast = CarsSaDesc.get_or_none(CarsSaDesc.cid == cid, CarsSaDesc.sa == i, CarsSaDesc.lang == 'zh',
                                                      CarsSaDesc.brand_code == audiPart.brand_code)
                if not contrast:
                    contrast = CarsSaDesc.get_or_none(CarsSaDesc.sa == i, CarsSaDesc.lang == 'zh',
                                                      CarsSaDesc.brand_code == audiPart.brand_code)
                if contrast and contrast.desc:
                    saDescout = remove_tt(contrast.desc)
                    # model_desc.append(contrast.desc)
                    if '发动机' in contrast.desc:
                        # 筛选出sa表desc中的排量信息
                        for key in pl:
                            if key in contrast.desc:
                                vb_desc.append(key)
                        for key in s:
                            if key in saDescout:
                                key = cleanCylinder(key)
                                cylinder.append(key)
                        for key in fuel:
                            if key in saDescout:
                                energy_type.append(key)
                        for key in glv:
                            if key in saDescout:
                                key = cleanPower(key)
                                power.append(key)

                    # 处理变速器配置
                    if ('变速箱' in contrast.desc or '变速器' in contrast.desc) and ('档' in contrast.desc or '无级' in contrast.desc):
                        for key in bsx:
                            if key in saDescout:
                                key = cleanTransmission(key)
                                transmission.append(key)

                    # 处理驱动配置
                    for key in qd:
                        if key in saDescout:
                            key = cleanDrive(key)
                            drive.append(key)

                    # 处理差速器配置
                    if '差速' in contrast.desc or '减速' in contrast.desc:
                        diff_mech.append(contrast.desc)

            for i in vb:
                if cid.isdigit():
                    vb_AF = Vb.get_or_none(Vb.aa == str(int(cid)), Vb.ab.contains(i), Vb.brand == audiPart.brand_code)
                elif cid.startswith('U'):
                    vb_AF = Vb.get_or_none(Vb.aa == cid[1:], Vb.ab.contains(i), Vb.brand == audiPart.brand_code)
                else:
                    vb_AF = Vb.get_or_none(Vb.aa == cid, Vb.ab.contains(i), Vb.brand == audiPart.brand_code)
                if vb_AF:
                    if vb_AF.af:
                        vb_desc.append(str(int(vb_AF.af) / 100))
                    if vb_AF.ai:
                        cylinder.append(vb_AF.ai)
                    if vb_AF.ag:
                        power.append(vb_AF.ag)

            # 处理part时间
            date, colorDate, year = audi_do_filter_extra_info(audiPart.extra_info)
            dateLeft, dateRight = mergeDate(date)
            colordateLeft, colordateRight = mergeDate(colorDate)
            yearLeft, yearRight = mergeDate(year)

            leftList = []
            if dateLeft:
                leftList.append(dateLeft)
            if colordateLeft:
                leftList.append(colordateLeft)
            if yearLeft:
                leftList.append(yearLeft)
            rightList = []
            if dateRight:
                rightList.append(dateRight)
            if colordateRight:
                rightList.append(colordateRight)
            if yearRight:
                rightList.append(yearRight)
            mdateLeft = max(leftList) if leftList else ''
            mdateRight = min(rightList) if rightList else ''
            merge_date = mdateLeft + ('-' if mdateLeft or mdateRight else '') + mdateRight

            vb_desc = set(vb_desc)
            vb_desc = list(vb_desc)
            vb_desc = ','.join(vb_desc)

            cylinder = set(cylinder)
            cylinder = list(cylinder)
            cylinder = ','.join(cylinder)

            transmission = set(transmission)
            transmission = list(transmission)
            transmission = ','.join(transmission)

            drive = set(drive)
            drive = list(drive)
            drive = ','.join(drive)

            diff_mech = set(diff_mech)
            diff_mech = list(diff_mech)
            diff_mech = ','.join(diff_mech)

            energy_type = set(energy_type)
            energy_type = list(energy_type)
            energy_type = ','.join(energy_type)
            power = set(power)
            power = list(power)
            power = ','.join(power)
            vb = ','.join(vb)

            ssa_codes = []
            # 发动机排量
            scylinder = []
            # 发动机代码
            svb = []
            # 发动机排量信息
            svb_desc = []
            stransmission = []
            sdrive = []
            sdiff_mech = []
            senergy_type = []
            spower = []
            merge_sdate = ''
            audiStructure = CarsStructureCollectionNew.get_or_none(CarsStructureCollectionNew.cid == audiPart.cid, CarsStructureCollectionNew.mid == audiPart.mid)

            # sSacodes = getSacode(audiStructure.sa_code)
            if audiStructure:
                structure_sa_codes = audiStructure.sa_code or '{}'
                structure_sa_codes = ujson.loads(structure_sa_codes)
                for key, value in structure_sa_codes.items():
                    if value:
                        ssa_codes = ssa_codes + value
                sModel = audiStructure.model.replace('<br/>', '')
                sDescription = audiStructure.description

                sModelout = remove_tt(sModel)
                sDescriptionout = remove_tt(sDescription)

                if sDescription:
                    for key in s:
                        if key in sDescriptionout:
                            key = cleanCylinder(key)
                            scylinder.append(key)
                    for key in pl:
                        if key in sDescription:
                            svb_desc.append(key)

                if sModel:
                    for key in bsx:
                        if key in sModelout:
                            key = cleanTransmission(key)
                            stransmission.append(key)
                    for key in qd:
                        if key in sModelout:
                            key = cleanDrive(key)
                            sdrive.append(key)

                    for key in s:
                        if key in sModelout:
                            key = cleanCylinder(key)
                            scylinder.append(key)
                    for key in pl:
                        if key in sModel:
                            svb_desc.append(key)
                    for key in fdjdm:
                        if key in sModel:
                            svb.append(key)
                    if 'PR:' in sModel or 'PR-' in sModel or 'pr:' in sModel or 'pr-' in sModel:
                        # 处理part_model中的PR
                        s_pr = re.sub('[\u4e00-\u9fa5]', '', sModel)
                        s_pr = s_pr.replace('PR:', '')
                        s_pr = s_pr.replace('PR-', '')
                        s_pr = s_pr.replace('+', ',')
                        s_pr = s_pr.replace('/', ',')
                        s_pr = s_pr.replace(':', ',')
                        s_pr = s_pr.replace('_', ',')
                        s_pr = s_pr.split(',')
                        for j in s_pr:
                            ssa_codes.append(j)

                for i in ssa_codes:
                    if cid.startswith('U'):
                        contrast = CarsSaDesc.get_or_none(CarsSaDesc.cid == cid[1:], CarsSaDesc.sa == i,
                                                          CarsSaDesc.lang == 'zh',
                                                          CarsSaDesc.brand_code == audiPart.brand_code)
                    else:
                        contrast = CarsSaDesc.get_or_none(CarsSaDesc.cid == cid, CarsSaDesc.sa == i,
                                                          CarsSaDesc.lang == 'zh',
                                                          CarsSaDesc.brand_code == audiPart.brand_code)
                    if not contrast:
                        contrast = CarsSaDesc.get_or_none(CarsSaDesc.sa == i, CarsSaDesc.lang == 'zh', CarsSaDesc.brand_code == audiPart.brand_code)
                    if contrast and contrast.desc:
                        ssaDescout = remove_tt(contrast.desc)
                        # model_desc.append(contrast.desc)
                        if '发动机' in contrast.desc:
                            # 筛选出sa表desc中的排量信息
                            for key in pl:
                                if key in contrast.desc:
                                    svb_desc.append(key)
                            for key in s:
                                if key in ssaDescout:
                                    key = cleanCylinder(key)
                                    scylinder.append(key)
                            for key in fuel:
                                if key in ssaDescout:
                                    senergy_type.append(key)
                            for key in glv:
                                if key in ssaDescout:
                                    key = cleanPower(key)
                                    spower.append(key)

                        # 处理变速器配置
                        if ('变速箱' in contrast.desc or '变速器' in contrast.desc) and ('档' in contrast.desc or '无级' in contrast.desc):
                            for key in bsx:
                                if key in ssaDescout:
                                    key = cleanTransmission(key)
                                    stransmission.append(key)

                        # 处理驱动配置
                        for key in qd:
                            if key in ssaDescout:
                                key = cleanDrive(key)
                                sdrive.append(key)

                        # 处理差速器配置
                        if '差速' in contrast.desc or '减速' in contrast.desc:
                            sdiff_mech.append(contrast.desc)

                for i in svb:
                    if cid.isdigit():
                        vb_AF = Vb.get_or_none(Vb.aa == str(int(cid)), Vb.ab.contains(i), Vb.brand == audiPart.brand_code)
                    elif cid.startswith('U'):
                        vb_AF = Vb.get_or_none(Vb.aa == cid[1:], Vb.ab.contains(i), Vb.brand == audiPart.brand_code)
                    else:
                        vb_AF = Vb.get_or_none(Vb.aa == cid, Vb.ab.contains(i), Vb.brand == audiPart.brand_code)
                    if vb_AF:
                        if vb_AF.af:
                            svb_desc.append(str(int(vb_AF.af) / 100))
                        if vb_AF.ai:
                            scylinder.append(vb_AF.ai)
                        if vb_AF.ag:
                            spower.append(vb_AF.ag)

                sdate, scolordate, syear = audi_do_filter_extra_info(audiStructure.extra_info)
                sdateLeft, sdateRight = mergeDate(sdate)
                scolordateLeft, scolordateRight = mergeDate(scolordate)
                syearLeft, syearRight = mergeDate(syear)
                # sleftList = [sdateLeft, scolordateLeft, syearLeft]
                # srightList = [sdateRight, scolordateRight, syearRight]
                sleftList = []
                if sdateLeft:
                    sleftList.append(sdateLeft)
                if scolordateLeft:
                    sleftList.append(scolordateLeft)
                if syearLeft:
                    sleftList.append(syearLeft)
                srightList = []
                if sdateRight:
                    srightList.append(sdateRight)
                if scolordateRight:
                    srightList.append(scolordateRight)
                if syearRight:
                    srightList.append(syearRight)
                msdateLeft = max(sleftList) if sleftList else ''
                msdateRight = min(srightList) if srightList else ''
                merge_sdate = msdateLeft + ('-' if msdateLeft or msdateRight else '') + msdateRight

            svb_desc = set(svb_desc)
            svb_desc = list(svb_desc)
            svb_desc = ','.join(svb_desc)

            scylinder = set(scylinder)
            scylinder = list(scylinder)
            scylinder = ','.join(scylinder)

            stransmission = set(stransmission)
            stransmission = list(stransmission)
            stransmission = ','.join(stransmission)

            sdrive = set(sdrive)
            sdrive = list(sdrive)
            sdrive = ','.join(sdrive)

            sdiff_mech = set(sdiff_mech)
            sdiff_mech = list(sdiff_mech)
            sdiff_mech = ','.join(sdiff_mech)

            senergy_type = set(senergy_type)
            senergy_type = list(senergy_type)
            senergy_type = ','.join(senergy_type)
            spower = set(spower)
            spower = list(spower)
            spower = ','.join(spower)
            svb = ','.join(svb)

            unit = {'pid': audiPart.pid, 'cid': audiPart.cid, 'mid': audiPart.mid, 'brand_code': audiPart.brand_code,
                    'standard_label': stand_label, 'engine_code': vb,
                    'cylinder': cylinder, 'transmission': transmission, 'drive': drive, 'diff_mech': diff_mech,
                    'displacement': vb_desc, 'pid_date': merge_date, 'energy_type': energy_type, 'power': power,
                    'group_engine_code': svb, 'group_cylinder': scylinder, 'group_transmission': stransmission,
                    'group_drive': sdrive, 'group_diff_mech': sdiff_mech, 'group_displacement': svb_desc,
                    'group_date': merge_sdate, 'group_energy_type': senergy_type, 'group_power': spower}
            print(unit)
            items.append(unit)

df = pd.DataFrame(items)
df['pid'] = df['pid'].astype('str')
df.to_csv('./export/skoda_parts.csv', mode='a', index=False)